<?php
namespace app\index\controller;
use alimsg\Msg;
use app\index\model\User;
use think\Controller;
use think\captcha\Captcha;
require_once("../vendor/qqConnect/API/qqConnectAPI.php");
class Login extends Controller
{
    public function index()
    {
        $this->assign('title',"登录");
        return $this->fetch();
    }

    public function test()
    {
        $res = Msg::send('18925139194',mt_rand(1000,9999));

        var_dump($res);
    }

    public function check()
    {
    	$data =  input('post.');

    	$captcha = new Captcha();

    	if(!$captcha->check($data['captcha'])){
    		$this->error('验证码错误');
    	}

    	$user = User::get(['phone'=>$data['loginName']]);

    	if(!$user){
    		$user = User::get(['email'=>$data['loginName']]);
    	}

    	if( $user && $user->password==md5($data['password'])){

    		session('user_id',$user->user_id);
    		session('face',$user->face);
    		session('nickname',$user->nickname);

    		$this->success('登录成功');

    	}else{
    		$this->error('登录名与密码不正确');
    	}
    }

    public function out()
    {
       session(null);
       $this->success("退出成功",url('index/login/index'));
    }

    public function qqlogin()
    {
        $qc = new \QC();
        $qc->qq_login();
        exit;
    }

    public function qqreturn()
    {
        $qc = new \QC();
        $qc->qq_callback();
        $openid  = $qc->get_openid();
        
        $user = User::get(['openid'=>$openid]);

        if($user){
            session('user_id',$user->user_id);
            session('face',$user->face);
            session('nickname',$user->nickname);
            $this->success("QQ登录成功",url('index/user/set'));
        }else{

            $qc = new \QC();
            $userInfo = $qc->get_user_info();

            $userInfo['sex'] = $userInfo['gender'];
            $userInfo['openid'] = $openid;
            $userInfo['face'] = $this->get_img($userInfo['figureurl_qq_2']);


            $user = User::create($userInfo);

            if($user){
                session('user_id',$user->user_id);
                session('face',$user->face);
                session('nickname',$user->nickname);
                $this->success("第一次登录，自动创建帐号。并且登录 ",url('index/user/set'));
            }else{
                $this->error('QQ登录失败');
            }
        }
    }


    public function get_img($url='')
    {
        // $url = "http://qzapp.qlogo.cn/qzapp/101521602/11471DA1D76577ECE09C68A51047B528/100";

        $ch = curl_init();//初始化

        curl_setopt($ch,CURLOPT_URL,$url);

        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        curl_setopt($ch,CURLOPT_HEADER,0);

        $output = curl_exec($ch);//执行请求

        $dir = 'uploads/'.date("Ymd");

        is_dir($dir) || mkdir($dir);

        $face = $dir.'/'.uniqid().".png";

        file_put_contents($face,$output);

        return  $face;
    }

    public function weixin(){

        session(null);

        $url = "http://1o533n8683.imwork.net".url("index/login/weixin_return");
        
        // $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbac82045a637c02e&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbac82045a637c02e&redirect_uri={$url}&response_type=code&scope=snsapi_base&state=123#wechat_redirect";

        header('Location:'.$url);
            exit;
    }

    public function weixin_return()
    {
        
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxbac82045a637c02e&secret=7c320fea54e77a01fcc59ff7060b04e2&code=".input('code')."&grant_type=authorization_code";

        $data = file_get_contents($url);

        $data = json_decode($data,true);

        $openid = $data['openid'];

        $user = User::get(['openid'=>$openid]);

        

        if($user){
            session('user_id',$user->user_id);
            session('face',$user->face);
            session('nickname',$user->nickname);

            // header("Location:".url('index/user/set'));
            $this->success("QQ登录成功",url('index/user/set'));
        }else{

            $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$data['access_token']."&openid=".$data['openid']."&lang=zh_CN";

            $user = file_get_contents($url);

            $user = json_decode($user,true);

            $userInfo['nickname'] = $user['nickname'];
            $userInfo['sex'] = $user['sex'] ? '男':'女';
            $userInfo['openid'] = $user['openid'];
            $userInfo['city'] = $user['city'];
            $userInfo['face'] = $this->get_img($user['headimgurl']);

            $user = User::create($userInfo);

            if($user){
                session('user_id',$user->user_id);
                session('face',$user->face);
                session('nickname',$user->nickname);
                $this->success("第一次登录，自动创建帐号。并且登录 ",url('index/user/set'));
            }else{
                $this->error('QQ登录失败');
            }
        }
    }
}